namespace CRMG.UserManagement.Source.DL
{
    using System;
    using Guru.Framework.BaseDal;
    using Guru.Framework.Util;
    using CRMG.UserManagement.Source.BO;
    using System.Data;
    using CRMG.UserManagement.Source.QO;

    public class UserDL : BaseDL
    {

        public UserDL()
        {
        }

        public DataTable GetUserList()
        {
            try
            {
                return Manager.GetResultSet(UserCrud.GetUserList()).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
        }

        public User GetUser(string username, string pass)
        {
            try
            {
                User usr = new User()
                {
                    UserName = username,
                    Pass = pass,
                    IsActive = 1
                };

                DataTable dt = GetWhereChangeColumnList(usr);

                usr = null;
                DataRow row = dt.Rows[0];
                usr = new User();
                usr.OBJID = ConverterUtil.Convert2Int(row["OBJID"]);
                usr.FirstName = row["FirstName"].ToString();
                usr.LastName = row["LastName"].ToString();
                usr.UserTypeId = ConverterUtil.Convert2Int(row["UserTypeId"]);
                usr.UserName = row["UserName"].ToString();
                usr.Pass = "";
                usr.IdentityNo = row["IdentityNo"].ToString();
                usr.EMail = row["EMail"].ToString();
                usr.Phone = row["Phone"].ToString();
                usr.Address = row["Address"].ToString();
                usr.UserCreationDate = ConverterUtil.ConvertString2DateTime(row["UserCreationDate"].ToString());
                usr.UpdateUserId = ConverterUtil.Convert2Int(row["UpdateUserId"]);
                usr.UpdateTime = ConverterUtil.ConvertString2DateTime(row["UpdateTime"].ToString());
                usr.IsActive = ConverterUtil.Convert2Int(row["IsActive"]);

                return usr;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public User GetUser(int userId)
        {
            try
            {
                User usr = new User()
                {
                    OBJID = userId,
                    IsActive = 1
                };

                DataTable dt = GetWhereChangeColumnList(usr);
                usr = null;
                foreach (DataRow row in dt.Rows)
                {
                    usr = new User();
                    usr.OBJID = ConverterUtil.Convert2Int(row["OBJID"]);
                    usr.FirstName = row["FirstName"].ToString();
                    usr.LastName = row["LastName"].ToString();
                    usr.UserTypeId = ConverterUtil.Convert2Int(row["UserTypeId"]);
                    usr.UserName = row["UserName"].ToString();
                    usr.Pass = "";
                    usr.IdentityNo = row["IdentityNo"].ToString();
                    usr.EMail = row["EMail"].ToString();
                    usr.Phone = row["Phone"].ToString();
                    usr.Address = row["Address"].ToString();
                    usr.UserCreationDate = ConverterUtil.ConvertString2DateTime(row["UserCreationDate"].ToString());
                    usr.UpdateUserId = ConverterUtil.Convert2Int(row["UpdateUserId"]);
                    usr.UpdateTime = ConverterUtil.ConvertString2DateTime(row["UpdateTime"].ToString());
                    usr.IsActive = ConverterUtil.Convert2Int(row["IsActive"]);
                }

                return usr;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable GetTrainers()
        {
            try
            {
                return Manager.GetResultSet(UserCrud.GetTrainers()).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}

